﻿using System;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
using System.Data.SQLite;

namespace Homework11
{
    public partial class Form1 : Form
    {
        SQLiteConnection cn;
        string[] c_word = new string[300]; //存中文
        int length_of_chinese_word;  //单词数量
        int current_num_of_chinese = 1;
        public Form1()
        {
            InitializeComponent();
            createWordDatabase();
            initializeWordDatabase();
            getChineseWord(); //得到包含所有中文释义的数组，并且每一次都是随机排序的。
            chinese_word.Text = c_word[0];

        }

        //创建单词数据库
        void createWordDatabase()
        {
            SQLiteConnection.CreateFile("word.sqlite");
            //建立连接
            cn = new SQLiteConnection("Data Source=word.sqlite;Version=3;");
            //创建一个table
            if (cn.State != System.Data.ConnectionState.Open)
            {
                cn.Open();
                SQLiteCommand cmd = new SQLiteCommand();
                cmd.Connection = cn;
                cmd.CommandText = "CREATE TABLE IF NOT EXISTS word(english varchar(30),chinese varchar(10))";//输入SQL语句
                cmd.ExecuteNonQuery();
            }
            cn.Close();

        }

        //数据库单词初始化
        void initializeWordDatabase()
        {
            cn.Open();
            SQLiteCommand cmd = new SQLiteCommand();
            cmd.Connection = cn;
            cmd.CommandText = "insert into word (english, chinese) values ('sincerely', '真诚地')";
            cmd.ExecuteNonQuery();
            cmd.CommandText = "insert into word (english, chinese) values ('bloom', '开花')";
            cmd.ExecuteNonQuery();
            cmd.CommandText = "insert into word (english, chinese) values ('rose', '玫瑰')";
            cmd.ExecuteNonQuery();
            cmd.CommandText = "insert into word (english, chinese) values ('echo', '回声')";
            cmd.ExecuteNonQuery();
            cmd.CommandText = "insert into word (english, chinese) values ('coffee', '咖啡')";
            cmd.ExecuteNonQuery();
            cmd.CommandText = "insert into word (english, chinese) values ('loveliness', '美好')";
            cmd.ExecuteNonQuery();
            cn.Close();

        }

        //得到中文释义数组
        void getChineseWord()
        {
            cn.Open();
            SQLiteCommand cmd = new SQLiteCommand();
            cmd.Connection = cn;
            cmd.CommandText = "select chinese from word";
            SQLiteDataReader reader = cmd.ExecuteReader();
            int i = 0;
            while (reader.Read())
            {
                c_word[i] = (string)reader["chinese"];
                i++;
            }
            length_of_chinese_word = i;
            cn.Close();
            //对数组进行随机排序，保证每次背单词都是乱序的。
            c_word = RandomArr(c_word, length_of_chinese_word);
        }

        //数组随机排序函数
        string[] RandomArr(string[] arr, int length)
        {
            Random r = new Random();
            for (int i = 0; i < length; i++)
            {
                int index = r.Next(length);//随机获得0（包括0）到arr.Length的索引
                string temp = arr[i];  //当前元素和随机元素交换位置
                arr[i] = arr[index];
                arr[index] = temp;
            }
            return arr;
        }

        //确认按钮
        private void button1_Click(object sender, EventArgs e)
        {
            
            cn.Open();
            SQLiteCommand cmd_select = new SQLiteCommand();
            cmd_select.Connection = cn;
            cmd_select.CommandText = "select english from word where chinese='" + chinese_word.Text + "'";
            SQLiteDataReader reader = cmd_select.ExecuteReader();
            while (reader.Read())
            {
                if (textBox1.Text == (string)reader["english"])
                {
                    
                    label4.Text = "答对啦！！！";
                }
                else
                {
                    label4.Text = "错误，正确的单词是："+ (string)reader["english"];
                }
            }
            cn.Close();
        }

        //下一个按钮
        private void next_button_Click(object sender, EventArgs e)
        {
            label4.Text = "";
            textBox1.Text = "";
            chinese_word.Text = c_word[current_num_of_chinese];
            current_num_of_chinese += 1;

            //背完所有时提示
            if (current_num_of_chinese > length_of_chinese_word)
            {
                label3.Text = "恭喜你！背完了所有单词！";
            }
        }

        //我不会按钮
        private void unknow_button_Click(object sender, EventArgs e)
        {
            cn.Open();
            SQLiteCommand cmd_select = new SQLiteCommand();
            cmd_select.Connection = cn;
            cmd_select.CommandText = "select english from word where chinese='" + chinese_word.Text + "'";
            SQLiteDataReader reader = cmd_select.ExecuteReader();
            while (reader.Read())
            {
                label4.Text = "正确的单词是：" + (string)reader["english"];
            }
            cn.Close();
        }
    }
}
